# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2JsonByYM
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

import time

localTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
"""
表名：zzh_LeveL

说明：省份表
"""


class LeveLDaoYM(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def queryLeveLById(self, id):

        sql = "SELECT * FROM zzh_level WHERE level_id=%s"
        cur = self.conn.cursor()
        total_count = cur.execute(sql, [id])
        if total_count:
            result = cur.fetchall()
            cur.close()
            self.conn.close()
            userOauthJson = Table2JsonByYM.LeveL_list_to_json(result)
            return total_count, userOauthJson
        else:
            cur.close()
            self.conn.close()
            return 0, None

    def queryLeveLByCondition(self, **kwargs):

        LeveLName = kwargs['LeveLName']
        page_index = kwargs['pageIndex']
        PAGE_SIZE = kwargs['pageSize']
        cur = self.conn.cursor()
        if LeveLName is None:
            sql="SELECT * FROM zzh_level"
            total_count = cur.execute(sql)
            if total_count:
                result = cur.fetchall()
                cur.close()
                self.conn.close()
                userOauthJSON = Table2JsonByYM.LeveL_list_to_json(result)
                return total_count, userOauthJSON

        else:
            sql = "SELECT * FROM zzh_level WHERE level_name=%s "
            total_count = cur.execute(sql, [LeveLName])
            if total_count:
                result = cur.fetchall()
                cur.close()
                self.conn.close()
                if (page_index * PAGE_SIZE) < total_count:
                    start = (page_index - 1) * PAGE_SIZE
                    end = page_index * PAGE_SIZE
                    userOauthPart = result[start: end]
                    userOauthJSON = Table2JsonByYM.LeveL_list_to_json(userOauthPart)
                    return total_count, userOauthJSON
                else:
                    start = (page_index - 1) * PAGE_SIZE
                    userOauthPart = result[start:]
                    userOauthJSON = Table2JsonByYM.LeveL_list_to_json(userOauthPart)
                    return total_count, userOauthJSON

            else:
                cur.close()
                self.conn.close()
                return 0, None


    def insertLeveL(self, Info):


        LeveLName = Info['LeveLName']  #
        LeveLCode = Info['LeveLCode']

        cur = self.conn.cursor()
        sql = 'INSERT INTO zzh_level (level_name,level_code) VALUES (%s,%s )'
        rs = cur.execute(sql, [LeveLName, LeveLCode])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None


    def selectLeveL(self, id):
        """
        根据ID查询信息，返回元组

        """
        sql = "SELECT * FROM zzh_level WHERE level_id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [id])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr


    def updateLeveL(self, Info):  #

        id = Info['id']  # 必填，请传递user_id
        LeveLName = Info['LeveLName']  #
        LeveLCode = Info['LeveLCode']

        cur = self.conn.cursor()
        sql = 'UPDATE zzh_level SET level_name=%s,level_code=%s   WHERE level_id=%s'
        rs = cur.execute(sql, [LeveLName, LeveLCode, id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        if rs:
            return rs
        else:
            return None

    def deleteLeveL(self, ids):

        sql = "delete from zzh_level WHERE level_id=%s"
        cur = self.conn.cursor()
        for id in ids:
            rs = cur.execute(sql, [id])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs
